package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.mall.dto.FrontBrandDto;
import com.mall.dto.FrontIndexProductDto;
import com.mall.dto.PmsBrandParam;
import com.mall.entity.PmsBrand;
import com.mall.entity.PmsProduct;
import com.mall.mapper.PmsBrandMapper;
import com.mall.mapper.PmsProductMapper;
import com.mall.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author zzx
 * @since 2022-06-15
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public List<PmsBrand> listAllBrand() {
        return list();
    }

    @Override
    public int createBrand(PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (pmsBrand.getFirstLetter().isEmpty()) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return save(pmsBrand) ? 1 : 0;
    }

    @Override
    public int updateBrand(Long id, PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (pmsBrand.getFirstLetter().isEmpty()) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        PmsProduct product = new PmsProduct();
        product.setBrandName(pmsBrand.getName());
        return productMapper.update(product, new QueryWrapper<PmsProduct>().eq("brand_id", id));
    }

    @Override
    public int deleteBrand(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int deleteBrand(List<Long> ids) {
        return remove(new QueryWrapper<PmsBrand>().in("id", ids)) ? 1 : 0;
    }

    @Override
    public List<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        if (keyword!=null) {
            if (!keyword.isEmpty()) {
                queryWrapper.like("name", keyword);
                queryWrapper.orderByDesc(keyword);
            }
        }
        return list(queryWrapper);
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return getById(id);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        return update(new PmsBrand(), new UpdateWrapper<PmsBrand>().in("id", ids).set("show_status", showStatus)) ? 1 : 0;
    }

    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        return update(new PmsBrand(), new UpdateWrapper<PmsBrand>().in("id", ids).set("factory_status", factoryStatus)) ? 1 : 0;
    }


    @Override
    public FrontBrandDto getFrontBrand(Long id) {
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("show_status", 1);
        PmsBrand pmsBrand = getOne(queryWrapper);
        QueryWrapper<PmsProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("brand_id", id);
        productQueryWrapper.eq("delete_status", 0);
        List<PmsProduct> pmsProductList = productMapper.selectList(productQueryWrapper);
        List<FrontIndexProductDto> frontIndexProductDtoList = new ArrayList<>();
        frontIndexProductDtoList = pmsProductList.stream().map(pmsProduct -> {
            FrontIndexProductDto frontIndexProductDto = new FrontIndexProductDto();
            frontIndexProductDto.setProduct(pmsProduct);
            return frontIndexProductDto;
        }).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        FrontBrandDto frontBrandDto = new FrontBrandDto();
        frontBrandDto.setBrand(pmsBrand);
        frontBrandDto.setProductList(frontIndexProductDtoList);
        return frontBrandDto;
    }


//    @Override
//    public FrontBrandListDto getAllBigPic() {
//        List<String> bigPicList = new ArrayList<>();
//        List
//        List<PmsBrand> pmsBrandList = list();
//        for (PmsBrand pmsBrand : pmsBrandList) {
//            if (pmsBrand.getBigPic()!=null && !pmsBrand.getBigPic().isEmpty() && pmsBrand.getShowStatus()==1) {
//                bigPicList.add(pmsBrand.getBigPic());
//            }
//        }
//        return bigPicList;
//    }
}
